package com.carp.service.Impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.carp.entity.CarpArticle;
import com.carp.mapper.CarpArticleMapper;
import com.carp.service.CarpIndexService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CarpIndexServiceImpl implements CarpIndexService {

    @Resource
    private CarpArticleMapper carpArticleMapper;

    //查询所有文章
    @Override
    public List<Integer> articleAll() {
        List<CarpArticle> carpArticles = carpArticleMapper.selectList(null);

        List<Integer> partMonth = this.getPartMonth(carpArticles);

        //所有文章数量,除第一季度,每个季度要加上前一个季度的数量
        ArrayList<Integer> integers = new ArrayList<>();
        int q1 = partMonth.get(0),q2 = partMonth.get(1),q3 = partMonth.get(2),q4 = partMonth.get(3);
        integers.add(q1);
        integers.add(q1+q2 );
        integers.add(q1+q2+q3);
        integers.add(q1+q2+q3+q4);

        return integers;
    }

    //查询个人的文章
    @Override
    public List<Integer> personalArticle(Integer uid) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("uid",uid);

        List<CarpArticle> carpArticles = carpArticleMapper.selectByMap(map);
        List<Integer> partMonth = this.getPartMonth(carpArticles);

        return partMonth;
    }

    /**
     *每个季度统计方法(本应放入utils工具包中)
     */
    public List<Integer> getPartMonth(List<CarpArticle> carpArticles){
        //根据创建时间,区分每个季度
        int q1,q2,q3,q4;
        q1 = q2 = q3 = q4 = 0;
        for (CarpArticle carpArticle : carpArticles) {
            Quarter quarter = DateUtil.quarterEnum(carpArticle.getCreateTime());
            switch (quarter){
                case Q1: q1 ++;break;
                case Q2: q2 ++;break;
                case Q3: q3 ++;break;
                case Q4: q4 ++;break;
                default: break;
            }
        }
        //存入链表
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(q1);
        integers.add(q2);
        integers.add(q3);
        integers.add(q4);

        return integers;
    }
}
